open ExtLib
open Common

class type disposable = object
	method dispose : unit -> unit
end
					
class type supportDispose = object
	method as_disposable : disposable option
end

let dispose obj =
	obj#dispose()
		
(* obj -> (obj -> 'a) -> 'a *)
let using obj f =
	do_finally_p2 f dispose obj

					
class ['a] disposableContainer (o:'a) disposer = object(self)
	val mutable _o = Some(o)

	method as_disposable = Some(self:>disposable)
		
	method dispose() =
		if not(self#is_disposed) then (
			disposer self#value;
			_o <- None
		)
	method is_disposed =
		not (Option.is_some _o)
			
	method value =
		match _o with Some(o) -> o | None -> failwith "Disposed"
end

let get_value container =
	container#value
